package org.iabako.server.servlet;

import org.apache.poi.util.IOUtils;
import org.iabako.server.serviceimpl.security.SessionUser;
import org.iabako.shared.entity.user.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * Created by jose on 09/02/15.
 */
public class GetEnterpriseLogo extends HttpServlet {

    @Autowired
    SessionUser sessionUser;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Enterprise enterprise = sessionUser.getUser().getEnterprise();
        enterprise = enterprise.getParentEnterprise() != null ? enterprise.getParentEnterprise() : enterprise;

        if (enterprise.getLogo() == null) {
            return;
        }

        IOUtils.copy(new ByteArrayInputStream(enterprise.getLogo()), response.getOutputStream());
    }
}
